package com.feige.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.dao.AskForLeaveMapper;
import com.feige.dao.SignInMapper;
import com.feige.openfeigh.UserCourseService;
import com.feige.pojo.SignIn;
import com.feige.pojo.vo.UserVo;
import com.feige.result.R;
import com.feige.service.SignInService;
import com.feige.result.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 签到表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Service
@Slf4j
public class SignInServiceImpl extends ServiceImpl<SignInMapper, SignIn> implements SignInService {


    @Resource
    private AskForLeaveMapper askForLeaveMapper;

    @Resource
    private SignInMapper signInMapper;

    @Resource
    private UserCourseService userCourseService;


    @Override
    public AjaxResult saveSignIn(SignIn signIn) {
        QueryWrapper<SignIn> wrapper = new QueryWrapper<>();
        wrapper
                .select("sign_user_id")
                .eq("sign_user_id",signIn.getSignUserId())
                .eq("sign_task_id", signIn.getSignTaskId());
        SignIn sign = this.getOne(wrapper);
        if (sign != null && StrUtil.isNotBlank(sign.getSignUserId())){
            return AjaxResult.error(5000,"您已签到，请不要重复操作！");
        }else {
            boolean save = this.save(signIn);
            if (save){
                return AjaxResult.success();
            }else {
                return AjaxResult.error(5000,"签到失败，请重新签到！");
            }
        }
    }

    @Override
    public AjaxResult getSignDetail(String courseId, String signTaskId,Long bt) {
        long begin = System.currentTimeMillis();
        // 通过课程ID查询已请假的学生，签到开始时间在请假开始时间和结束时间之间就认为该学生已请假
        List<UserVo> askForLeaveUserVos = askForLeaveMapper.getAuditNameByCourseId(courseId)
                .stream()
                .filter(userVo -> (userVo.getBeginTime().getTime() < bt && userVo.getEndTime().getTime() >= bt))
                .peek(userVo -> {
                    userVo.setFlag(1);
                    userVo.setBeginTime(null);
                    userVo.setEndTime(null);
                })
                .collect(Collectors.toList());
        // 通过任务ID获取已签到的人
        List<UserVo> signedBySignTaskId = signInMapper.getSignedBySignTaskId(signTaskId)
                .stream()
                .peek(userVo -> userVo.setFlag(2))
                .collect(Collectors.toList());
        // 查询课程的所有用户
        R<List<UserVo>> rL = userCourseService.getCourseUserVoByCourseId(courseId);
        if (rL.getCode() != 200){
            return AjaxResult.error(5050,"服务调用失败！");
        }
        List<UserVo> courseUserVoByCourseId = rL.getData()
                .stream()
                .peek(userVo -> userVo.setFlag(3))
                .collect(Collectors.toList());
        HashSet<UserVo> userVos = new HashSet<>();
        userVos.addAll(askForLeaveUserVos);
        userVos.addAll(signedBySignTaskId);
        userVos.addAll(courseUserVoByCourseId);
        AjaxResult success = AjaxResult.success(userVos);
        long end = System.currentTimeMillis();
        log.info("消耗时间：" + (end - begin) / 1000.0 + "s");
        return success;
    }
}
